﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace OG_UPDATETOOL
{
    public partial class Form1 : Form
    {
        delegate void UIDele(Form1 that);

        public static void ProcInfo(Form1 that)
        {
            if(that.controler.isSafeToOperatorDevice == false)
            {
                that.groupBox_operator.Enabled = false;
            }
            else
            {
                that.groupBox_operator.Enabled = true;
            }
            switch (that.controler.infoType)
            {
                case Controler.InfoType.INFO:
                    that.richTextBox_info.SelectionColor = Color.Black;
                    that.richTextBox_info.AppendText("[INFO]:");
                    that.richTextBox_info.AppendText(that.controler.info + "。\n");
                    that.richTextBox_info.Select(that.richTextBox_info.TextLength, 0);
                    that.richTextBox_info.ScrollToCaret();
                    break;
                case Controler.InfoType.NOTIFY:
                    that.richTextBox_info.SelectionColor = Color.Green;
                    that.richTextBox_info.AppendText("[NOTE]:");
                    that.richTextBox_info.AppendText(that.controler.info + "。\n");
                    that.richTextBox_info.Select(that.richTextBox_info.TextLength, 0);
                    that.richTextBox_info.ScrollToCaret();
                    break;
                case Controler.InfoType.WARN:
                    that.richTextBox_info.SelectionColor = Color.Brown;
                    that.richTextBox_info.AppendText("[WARN]:");
                    that.richTextBox_info.AppendText(that.controler.info + "。\n");
                    that.richTextBox_info.Select(that.richTextBox_info.TextLength, 0);
                    that.richTextBox_info.ScrollToCaret();
                    break;
                case Controler.InfoType.ERROR:
                    that.richTextBox_info.SelectionColor = Color.Red;
                    that.richTextBox_info.AppendText("[ERROR]:");
                    that.richTextBox_info.AppendText(that.controler.info + "。\n");
                    that.richTextBox_info.Select(that.richTextBox_info.TextLength, 0);
                    that.richTextBox_info.ScrollToCaret();
                    break;
                case Controler.InfoType.RAW:
                    that.richTextBox_info.SelectionColor = Color.Purple;
                    that.richTextBox_info.AppendText(that.controler.info);
                    that.richTextBox_info.Select(that.richTextBox_info.TextLength, 0);
                    that.richTextBox_info.ScrollToCaret();
                    break;
                default:
                    break;
            }
            that.progressBar_status.Value = that.controler.progressStatus;
            switch (that.controler.connectState)
            {
                case Controler.ConnectState.CLOSE:
                    that.button_serial_connect.Text = "打开";
                    break;
                case Controler.ConnectState.OPEN:
                    that.button_serial_connect.Text = "关闭";
                    break;
                case Controler.ConnectState.CONNECTED:
                    that.button_serial_connect.Text = "连接成功";
                    break;
                case Controler.ConnectState.DISCONNECT:
                    that.button_serial_connect.Text = "连接已断开";
                    break;
                default:
                    break;
            }
        }
        
        public Form1()
        {
            InitializeComponent();
            controler.onControlerInfoGened += Controler_onControlerInfoGened;
            this.groupBox_operator.Enabled = false;
        }

        #region 界面委托操作
        UIDele ud = ProcInfo;
        Controler controler = new Controler();
        private void Controler_onControlerInfoGened(object sender, EventArgs e)
        {
            this.Invoke(ud, this);
        }
        #endregion

        private void comboBox_serial_port_DropDown(object sender, EventArgs e)
        {
            var ret = SerialPort.GetPortNames();
            this.comboBox_serial_port.Items.Clear();
            foreach(var p in ret )
            {
                this.comboBox_serial_port.Items.Add(p);
            }
        }

        private void button_serial_connect_Click(object sender, EventArgs e)
        {
            switch (this.controler.connectState)
            {
                case Controler.ConnectState.CLOSE:
                    if(this.comboBox_serial_port.Text == "")
                    {
                        return;
                    }
                    this.controler.Start(this.comboBox_serial_port.Text);
                    break;
                case Controler.ConnectState.OPEN:
                    this.controler.Stop();
                    break;
                case Controler.ConnectState.CONNECTED:
                    this.controler.Stop();
                    break;
                case Controler.ConnectState.DISCONNECT:
                    this.controler.Stop();
                    break;
                default:
                    break;
            }
        }

        private void button_clearInfo_Click(object sender, EventArgs e)
        {
            this.richTextBox_info.Text = "";
        }

        private void button_op_firmware_select_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = ".bin|*.bin";
            var ret = ofd.ShowDialog();
            
            if(ret == DialogResult.OK)
            {
                this.textBox_op_firmware_path.Text = ofd.FileName;
                this.textBox_crcResult.Text =  controler.CaclCrc16(ofd.FileName).ToString("X");
            }
        }

        private void button_op_firmware_burn_Click(object sender, EventArgs e)
        {
            this.controler.burn(this.textBox_op_firmware_path.Text);
        }

        private void button_op_firmware_erase_Click(object sender, EventArgs e)
        {
            var ret = MessageBox.Show(this,"确认擦除固件吗？","擦除固件",MessageBoxButtons.OKCancel);
            if(ret == DialogResult.OK)
            {
                this.controler.erase();
            }
        }

        private void button_op_reboot_Click(object sender, EventArgs e)
        {
            this.controler.reboot();
        }

        private void button_op_jumpToApp_Click(object sender, EventArgs e)
        {
            this.controler.jumpToApp();
        }

        private void button_op_firmware_checkCrc_Click(object sender, EventArgs e)
        {
            this.controler.crcCheck();
        }

        private void button_op_firmware_burnCrc_Click(object sender, EventArgs e)
        {
            if(this.textBox_crcResult.Text != "")
            {
                this.controler.crcWrite((UInt16)controler.CaclCrc16(this.textBox_op_firmware_path.Text));
            }
        }

        private void button_readBootInfo_Click(object sender, EventArgs e)
        {
            this.controler.getBootInfo();
        }

        private void button_oneClickToBurn_Click(object sender, EventArgs e)
        {
            if (this.textBox_op_firmware_path.Text == "")
            {
                return;
            }

            if (controler.isOneClickBurnStart == false)
            {
                
                controler.oneClickBurnStart(this.textBox_op_firmware_path.Text);
            }
            else
            {
                controler.oneClickBurnStop();
            }
        }
    }
}
